@extends("backend.layout.main")
<script src="/adminlte/plugins/jQuery/jquery-2.2.3.min.js"></script>
@section("content")
    <!-- Main content -->
    <section class="content">
        <!-- Small boxes (Stat box) -->
        <div class="row">
            <div class="col-lg-10 col-xs-6">
                <div class="box">

                    <div class="box-header with-border">
                        <h3 class="box-title">角色列表</h3>
                    </div>
                    <div class="box-header with-border">
                        <a href="javascript:void(0);" class="btn btn-success creation"><span class="glyphicon glyphicon-plus"></span> 添加角色</a>
                    </div>


                    <!-- /.box-header -->
                    <div class="box-body">
                        <table class="table table-bordered">
                            <tbody>
                            <tr>
                                <th style="width: 10px">ID</th>
                                <th>角色名称</th>
                                <th>拥有权限</th>
                                <th>操作</th>
                            </tr>
                            @foreach($data as $item)
                                <tr>
                                    <td>{{$item->id}}</td>
                                    <td>{{$item->name}}</td>
                                    <td>
                                        @foreach($item['permissions'] as $permission)
                                            |{{$permission->description}}|
                                        @endforeach
                                    </td>
                                    <td>
                                        <a href="javascript:void(0);" class="btn btn-success editaction" data-role="{{$item}}">
                                            修改角色</a>
                                        <a href="javascript:void(0);" class="btn btn-primary"  onclick="del({{$item->id}})">
                                            删除角色</a>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                    {{$data->links()}}

                </div>
            </div>
        </div>
        {{--静态模态框--}}
        <div class="modal fade" id="modal">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                                    class="sr-only">Close</span></button>
                        <h4 class="modal-title"></h4>
                    </div>
                    <div class="modal-body">
                        <form action="" onsubmit="return false;" id="formAdd">
                            {{csrf_field()}}
                            <input type="hidden" name="id" id="id">
                            <div class="form-group">
                                <label for="">角色名</label>
                                <input type="text" name="name" id="name" class="form-control" placeholder="角色名称">
                            </div>
                            <div class="form-group">
                                <label for="">描述</label>
                                <input type="text" name="description" id="description" class="form-control" placeholder="角色描述">
                            </div>
                            <div class="form-group">
                                <label for="">绑定权限</label>
                                <div class="form-group">
                                    @foreach($permissions as $permission)
                                        <div class="checkbox" style="display: inline-block;width: 24%">
                                            <label>
                                                <input type="checkbox" name="permissions[]"
                                                       value="{{$permission->id}}" class="ck">
                                                {{$permission->name}}
                                            </label>
                                        </div>
                                    @endforeach
                                </div>
                            </div>

                            <div class="form-group pull-right">
                                <input type="submit" value="提交" onclick="save()" class="btn btn-success">
                                <input type="reset" id="reset" value="重置" class="btn btn-danger">
                            </div>

                            <div style="clear:both"></div>
                        </form>
                    </div>

                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
        <script>
            function clean() {
                $('#id').val('');
                $('#reset').click();
            }
            function del(id) {

                $.ajax({
                    url:"/backend/roles/delete",
                    type:"POST",
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': "{{csrf_token()}}"
                    },
                    data:{
                        'id':id,
                    },
                    success:function(data){
                        if(data.code == 0){
                            swalreload('删除成功','');
                        }
                    },
                    error:function(){
                        swal('请求超时，稍后再试', '', 'error');
                    }
                })
            }
            function save() {
                $.ajax({
                    url: '/backend/roles/store',
                    type: "POST",
                    data: new FormData($('#formAdd')[0]),
                    contentType: false,
                    processData: false,
                    dataType: 'Json',
                    success: function (data) {
                        if (data.code == 0) {
                            swalreload(data.message);
                        } else {
                            swal(data.message, '', 'error');
                        }
                    },
                    error: function () {
                        swal("链接超时", '', 'error');
                    }
                })
            }

            $('.creation').click(function () {
                clean();
                $('.modal-title').html("添加角色");
                $('#modal').modal('show');
            });

            $('.editaction').click(function () {
                clean();
                role = $(this).data('role');
                $('.modal-title').html("修改角色");
                $('#id').val(role.id);
                $('#name').val(role.name);
                $('#description').val(role.description);
                $.each(role.permissions,function(key,permission){
                    $(".ck").each(function(){
                        if($(this).val() == permission.id){
                            $(this).prop('checked',true);
                        }
                    });
                })
                $('#modal').modal('show');
            });
        </script>
    </section>
@endsection